import unittest

import _3_1_run_deap
from _3_1_run_deap import *

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(lineno)d: %(message)s')
logger = logging.getLogger(__name__)


class TestDeapCase(unittest.TestCase):

    def test_IndividualList(self):
        genes = IndividualList([1, 2, 3], fitness=IndividualFitness(values=(1,)), order_num=3, travel_time=1)
        self.assertEqual(genes.fitness.valid, True)
        genes1 = IndividualList([1, 2, 3], fitness=IndividualFitness(), order_num=3, travel_time=1)
        self.assertEqual(genes1.fitness.valid, False)

    def test_align_to_genes(self):
        aligned = [32, 20]
        no_work_index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 45, 46, 47]
        order_index = {'33': 29, '20': 41, '32': 30, '22': 39}
        order_hour = {'33': 2, '20': 2, '32': 2, '22': 4}
        genes = align_to_genes(aligned, no_work_index, order_index, order_hour)
        self.assertEqual(genes,
                         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0,
                          0, 0, 0, 0, 0, 32, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 0, 0, -1, -1, -1])

    def test_order_time_matched(self):
        ind1 = [4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 0, 0, -1, -1, -1]
        ind2 = [6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 32, 32, 0, 0, 0, 0, 0, 0, 0, 22, 22, 22, 22, 0, 0, 0, -1, -1]
        new_ind1, new_ind2 = order_time_matched(ind1, ind2)
        self.assertEqual(len(new_ind1), 49)
        self.assertEqual(len(new_ind2), 49)

    def test_get_inventory_range(self):
        inventory = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
                     1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1]
        order_index = 42
        min_index, max_index = get_inventory_range(inventory, order_index)
        self.assertEqual(min_index, 31)
        self.assertEqual(max_index, 46)

    def test_check_near_orders_range(self):
        yesterday = get_yesterday(2)
        load_data(yesterday)
        df_orders = pd.read_csv('/data/disp/' + yesterday + '/orders_0_0.csv')
        se = df_orders.iloc[0]
        start_time = se['start']
        stop_time = se['stop']
        result = check_near_orders_range(se, start_time, stop_time)
        self.assertEqual(result, True)

    def test_gen_inventory(self):
        _3_1_run_deap.orders_travel_lock = {33: 2, 20: 2}
        _3_1_run_deap.orders_delay_lock = {33: 1, 20: 1}
        ind = [4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0,
               0, 0, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 0, 0, -1, -1, -1]
        invent = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1,
                  1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1]
        new_invent = gen_inventory(ind, invent)
        self.assertEqual(new_invent,
                         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0,
                          0, 0,
                          1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, -1, -1, -1])

    def test_gen_near_inds(self):
        yesterday = get_yesterday()
        load_data(yesterday)
        inds = df_artisans['genes'].apply(create_individual)
        select_offset = 0
        near_inds = gen_near_inds(inds, select_offset)
        logger.debug(near_inds)

    def test_select_nearest_artisan(self):
        yesterday = get_yesterday()
        load_data(yesterday)
        inds = [
            [0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 9, 9, 9, 0, 0,
             0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1],
            [8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 60, 60,
             60, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
             -1, 0, 0, 0, 0, 0, 61, 61, 61, 61, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [19, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 103, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 99, 0, -1, -1, 0, 0, 0, 0, 0, 68, 68, 68, 0, -1, -1, -1],
            [26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 77, 77, 77, 0, -1, -1],
            [29, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 81, -1, -1, -1, -1],
            [30, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
             82, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 86, 86, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1],
            [33, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
             -1, -1, 0, 0, 0, 87, 87, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
            [35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 0, 0, 0, -1, -1],
            [38, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 96, 96, 0, 0, -1, -1, -1, -1, -1],
            [41, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 0, 0, 0, 0, -1, -1, -1],
            [44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
             -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 106, 0, 0, 0, 0, 0]]
        new_ids = select_nearest_artisan(inds)
        logger.debug(new_ids)
        self.assertGreaterEqual(len(new_ids), 1)

    def test_select_lock_2_hours(self):
        yesterday = get_yesterday()
        load_data(yesterday)
        df_artisans = pd.read_csv('/data/disp/' + yesterday + '/artisans_0_0.csv')
        individuals = df_artisans['genes'].apply(create_individual)
        k = 3
        sel_inds = select_lock_2_hours(individuals, k)
        logger.debug(sel_inds)
        self.assertGreaterEqual(len(sel_inds), 0)


if __name__ == '__main__':
    unittest.main()
